package com.owen.blog.controller;

import com.owen.blog.dto.BlogLabelDTO;
import com.owen.blog.service.BlogLabelService;
import com.owen.blog.vo.BlogLabelVO;
import com.owen.common.utils.PageUtils;
import com.owen.common.utils.Query;
import com.owen.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "文章标签")
@RequestMapping("/blog/label")
public class BlogLabelController {

    @Autowired
    private BlogLabelService blogLabelService;

    @GetMapping
    @RequiresPermissions("blog:label:list")
    @ApiOperation(value = "进入文章标签管理页面", notes = "进入文章标签管理页面", httpMethod = "GET")
    String manage() {
        return "blog/label/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("blog:label:list")
    @ApiOperation(value = "文章标签查询", notes = "文章标签查询", httpMethod = "GET")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<BlogLabelVO> blogLabelList = blogLabelService.list(query);
        int total = blogLabelService.count(query);
        PageUtils pageUtils = new PageUtils(blogLabelList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("blog:label:add")
    @ApiOperation(value = "进入新增文章标签页面", notes = "进入新增文章标签页面", httpMethod = "GET")
    String add() {
        return "blog/label/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("blog:label:edit")
    @ApiOperation(value = "进入文章标签编辑页面", notes = "进入文章标签编辑页面", httpMethod = "GET")
    String edit(@PathVariable("id") String id, Model model) {
        BlogLabelVO blogLabel = blogLabelService.get(id);
        model.addAttribute("blogLabel", blogLabel);
        return "blog/label/edit";
    }

    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("blog:label:add")
    @ApiOperation(value = "新增文章标签信息", notes = "新增文章标签信息", httpMethod = "POST")
    public R save(BlogLabelDTO dto) {
        if (blogLabelService.save(dto) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @PostMapping("/update")
    @ApiOperation(value = "修改文章标签信息", notes = "修改文章标签信息", httpMethod = "POST")
    @RequiresPermissions("blog:label:edit")
    public R update(BlogLabelDTO dto) {
        blogLabelService.update(dto);
        return R.ok();
    }

    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("blog:label:remove")
    @ApiOperation(value = "删除文章标签信息", notes = "删除文章标签信息", httpMethod = "POST")
    public R remove(String id) {
        if (blogLabelService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @PostMapping("/batchRemove")
    @RequiresPermissions("blog:label:batchRemove")
    @ApiOperation(value = "批量删除文章标签信息", notes = "批量文章标签信息", httpMethod = "POST")
    public R remove(@RequestParam("ids[]") String[] ids) {
        blogLabelService.batchRemove(ids);
        return R.ok();
    }

}

